package cn.softgame.cms.secure.syspro.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.softgame.cms.common.page.domain.PageResultBean;
import cn.softgame.cms.open.common.service.CommonService;
import cn.softgame.cms.secure.syspro.domain.SysPubProModel;
import cn.softgame.cms.secure.syspro.service.SysPubService;

/**
 * @author 李莉
 * 下午03:54:07
 * TODO 系统公共属性的控制器
 */

@Controller
@RequestMapping("/secure/syspro/*")
public class SysPubController {
	@Autowired
	private SysPubService service;
	
	@Autowired
	private CommonService commonService;
	
	@RequestMapping(value="init.html")
	public ModelAndView init(){
		ModelAndView mav = new ModelAndView();
		mav.addObject("sysModel",new SysPubProModel());
		mav.setViewName("secure/syspro/sysProForm");
		return mav;
	}
	
	/**
	 * 系统属性列表
	 * @return
	 */
	@RequestMapping(value="listSysModel.html")
	public ModelAndView listDevModel() {
		ModelAndView mav = new ModelAndView();
		mav.addObject("sysModel",new SysPubProModel());
		mav.setViewName("secure/syspro/listSysProModel");
		return mav;
	}
	
	@RequestMapping(value="loadAllSysModels.html")
	public @ResponseBody PageResultBean loadAllSysModels(Integer jtStartIndex,Integer jtPageSize,String propName,String propCode){
		PageResultBean pr = new PageResultBean();
		try {
			SysPubProModel model = new SysPubProModel();
			model.setPropName(propName);
			model.setPropCode(propCode);
			model.setFromNumber(jtStartIndex);
			model.setToNumber(jtStartIndex+jtPageSize);
			pr.setResult("OK");
			pr.setRecords(service.qrySysPubModels(model));
			pr.setTotalRecordCount(service.qryTotalRecords(model));
		} catch (Exception e) {
			e.printStackTrace();
			pr.setResult("ERROR");
		}
		
		return pr;
	}
	
	@RequestMapping(value="save.html")
	public @ResponseBody PageResultBean save(SysPubProModel sysModel){
		PageResultBean pr = new PageResultBean();
		try {
			if(sysModel!=null){
				String propId = sysModel.getPropId();
				if(propId!=null && !"".equals(propId)){
					service.update(sysModel);
				}else{
					sysModel.setPropId(commonService.getCode());
					service.save(sysModel);
				}
			}
			pr.setResult("ok");
			pr.setRecord(sysModel);
		} catch (Exception e) {
			e.printStackTrace();
			pr.setResult("ERROR");
			pr.setMessage("保存失败!");
		}
		return pr;
	}
	
	@RequestMapping(value="delete.html")
	public @ResponseBody PageResultBean delete(String propId){
		PageResultBean pr = new PageResultBean();
		try {
			service.delete(propId);
			pr.setResult("ok");
		} catch (Exception e) {
			e.printStackTrace();
			pr.setResult("ERROR");
			pr.setMessage("删除失败!");
		}
		return pr;
	}
	
	@RequestMapping(value="qryById.html")
	public ModelAndView qryById(String propId){
		ModelAndView mav = new ModelAndView();
		mav.addObject("sysModel",service.qrySysByModelId(propId));
		mav.setViewName("secure/syspro/sysProForm");
		return mav;
	}
}
